package com.sc.nft.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.ClassifyType;
import com.sc.nft.enums.CommodityType;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.*;
import com.sc.nft.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class HomePageServiceImpl implements HomePageService {
    private final RedisUtil redisUtil;
    private final ProductStockDao productStockDao;
    private final UserInfoService userInfoService;
    private final DigitalCollectionProductInfoService digitalCollectionProductInfoService;
    private final AdvertisementInfoDao advertisementInfoDao;
    private final NoticeInfoDao noticeInfoDao;
    private final AppVersionService appVersionService;
    private final DigitalCollectionDao digitalCollectionDao;
    private final MallBannerService mallBannerService;
    private final DigitalCollectionProductInfoSubscribeDao digitalCollectionProductInfoSubscribeDao;
    private final DigitalCollectionProductInfoAuctionDao digitalCollectionProductInfoAuctionDao;
    private final UserInfoDao userInfoDao;

    @Override
    public HomePageVO homePageByiOS(String appStoreVersion) {
        HomePageVO homePageVO=new HomePageVO();

        Boolean isSm=false;
        int isIosShow = 0;
        UserInfo user = UserPool.getUser();
        if (ObjectUtil.isNotEmpty(user)){
            isSm=user.getIsSm();
            if(user.getUserTel().equals("13849904109") ){
                isIosShow =1;
            }
        }

        if(StrUtil.isNotBlank(appStoreVersion)){
            AppVersion ios = appVersionService.findOneByType("ios");
            if(ios.getAppStoreVersion().equals(appStoreVersion)){
                isIosShow =1;
            }
        }
        //公告
        List<NoticeInfoVO> noticeInfos = Lists.newArrayList();
        List<MallBannerVO> newBanner = new ArrayList<>();
        if(isIosShow ==1){

//            //滚动广告图
//            AdvertisementInfoVO advertisementInfoVO = new AdvertisementInfoVO();
//            advertisementInfoVO.setId(0L);
//            advertisementInfoVO.setType(1);
//            advertisementInfoVO.setImgUrl("http://thap.bee360.com.cn/scnft/h5_default/ios.jpg");
//            advertisementInfoVO.setReUrl("");
//            list.add(advertisementInfoVO);

            // 这里是新版本的banner图
            MallBannerVO mallBannerVO=new MallBannerVO();
            mallBannerVO.setId(0L);
            mallBannerVO.setRichText("");
            mallBannerVO.setLocation(1);
            mallBannerVO.setName("");
            mallBannerVO.setRemark("");
            mallBannerVO.setImgUrl("http://thap.bee360.com.cn/scnft/h5_default/ios.jpg");
            mallBannerVO.setSort(0);
            mallBannerVO.setSkipType(2);
            mallBannerVO.setSkipPage(0);
            mallBannerVO.setStatus(true);
            newBanner.add(mallBannerVO);
            //公告
            NoticeInfoVO noticeInfoVO = new NoticeInfoVO();
            noticeInfoVO.setId(0L);
            noticeInfoVO.setCreateTime(DateTime.now());
            noticeInfoVO.setNoticeTitle("玛特元宇宙即将上线,敬请期待");
            noticeInfoVO.setNoticeContent("");
            noticeInfoVO.setNoticeType(1);
            noticeInfos.add(noticeInfoVO);
        }else{
            //滚动广告图
//            List<AdvertisementInfo> advertisementInfos = advertisementInfoDao.getAdvertisementInfos();
//            for (AdvertisementInfo advertisementInfo : advertisementInfos) {
//                list.add(BeanUtil.toBean(advertisementInfo, AdvertisementInfoVO.class));
//            }
            newBanner = mallBannerService.list(1L, 1);
            //公告
             noticeInfos =noticeInfoDao.getNoticeInfos();
        }


        //盲盒系列
        List<ALLCatenaVO> catenaList= Lists.newArrayList();
//        List<ALLCatenaVO> catenaList= digitalCollectionProductInfoService.findBlindBoxCatena(isIosShow);
//        for (ALLCatenaVO allCatenaVO : catenaList) {
//            ProductStock productStock = productStockDao.getProductStock(allCatenaVO.getBlindBoxId(),allCatenaVO.getRelationId(),null);
//            allCatenaVO.setProductCount(productStock.getSumCount());
//            List timeList=new ArrayList();
//            if (ObjectUtil.isNotEmpty(allCatenaVO.getBeginTime())){
//                Date date = DateUtil.parse(allCatenaVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
//                allCatenaVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
//                String format = DateUtil.format(date, "MM-dd HH:mm");
//                timeList.add(format+" 发售");
//            }
//            if (ObjectUtil.isNotEmpty(allCatenaVO.getPriorityPurchaseBeginTime())){
//                Date date = DateUtil.parse(allCatenaVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
//                allCatenaVO.setPriorityPurchaseBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
//                String format = DateUtil.format(date, "MM-dd HH:mm");
//                timeList.add(format+" 优先购");
//            }
//            if (ObjectUtil.isNotEmpty(allCatenaVO.getRushBuyBeginTime())){
//                Date date = DateUtil.parse(allCatenaVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
//                allCatenaVO.setRushBuyBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
//                String format = DateUtil.format(date, "MM-dd HH:mm");
//                timeList.add(format+" 优先抢");
//            }
//            allCatenaVO.setList(timeList);
//        }
        //首页推荐商品
        List<HomePageDigitalCollectionProductVO> top10DigitalCollection= digitalCollectionProductInfoService.getTop10DigitalCollection(isIosShow);
        for (HomePageDigitalCollectionProductVO digitalCollectionProductVO : top10DigitalCollection) {

            if(digitalCollectionProductVO.getTypeClassify() == ClassifyType.COLLECTION){
                DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductVO.getRelationId());
                digitalCollectionProductVO.setProductCount(digitalCollection.getSumCount());
            }else{
                ProductStock productStock = productStockDao.getProductStock(digitalCollectionProductVO.getProductId(),digitalCollectionProductVO.getRelationId(),null);
                digitalCollectionProductVO.setProductCount(productStock.getSumCount());
            }

            List timeList=new ArrayList();
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 发售");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getPriorityPurchaseBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setPriorityPurchaseBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先购");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getRushBuyBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setRushBuyBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先抢");
            }
            digitalCollectionProductVO.setList(timeList);
        }
//        homePageVO.setAdvertisementInfos(list);
        //todo 2023年5月17日16:34:31新版banner 2个版本后这个上面的需要删除掉
        homePageVO.setNewBanner(newBanner);
        homePageVO.setNoticeInfos(noticeInfos);
        homePageVO.setCatenaList(catenaList);
        homePageVO.setTop10DigitalCollection(top10DigitalCollection);


        homePageVO.setIsSm(isSm);
        UserHomeVO userHome = userInfoService.getUserHome();
        homePageVO.setUserHomeVO(userHome);
        return homePageVO;
    }

    @Override
    public HomeUserPageVO homeUserPageIOS() {
        HomeUserPageVO result = new HomeUserPageVO();
        Boolean isSm=false;
        UserInfo user = UserPool.getUser();
        if (ObjectUtil.isNotEmpty(user)){
            isSm=user.getIsSm();

        }
        UserHomeVO userHome = userInfoService.getUserHome();
        result.setUserHomeVO(userHome);
        result.setIsSm(isSm);
        return result;
    }

    @Override
    public BannerHomeListVO bannerHomePageIOS(String appStoreVersion,Long location) {
        BannerHomeListVO result = new BannerHomeListVO();
        Integer isIosShow = this.getIsShowIOS(appStoreVersion);
        List<MallBannerVO> newBanner = new ArrayList<>();
        if (isIosShow.intValue() == 1){
            MallBannerVO mallBannerVO=new MallBannerVO();
            mallBannerVO.setId(0L);
            mallBannerVO.setRichText("");
            mallBannerVO.setLocation(1);
            mallBannerVO.setName("");
            mallBannerVO.setRemark("");
            mallBannerVO.setImgUrl("http://thap.bee360.com.cn/scnft/h5_default/ios.jpg");
            mallBannerVO.setSort(0);
            mallBannerVO.setSkipType(2);
            mallBannerVO.setSkipPage(0);
            mallBannerVO.setStatus(true);
            newBanner.add(mallBannerVO);
        }
//        else {
//            Object object = redisUtil.get(RedisKeyEnum.HOME_BANNER.getKey());
//            if (ObjectUtil.isNotEmpty(object)){
//                List mallBanners = (List) JSONObject.parseArray(object.toString());
//                for (Object banner : mallBanners) {
//                    MallBannerVO mallBannerVO = JSONObject.parseObject(banner.toString(), MallBannerVO.class);
//                    newBanner.add(mallBannerVO);
//                }
//                result.setNewBanner(newBanner);
//                return result;
//            }
//            else {
//
//
//                String data = JSONObject.toJSON(newBanner).toString();
//                redisUtil.set(RedisKeyEnum.HOME_BANNER.getKey(),data,30);
//                result.setNewBanner(newBanner);
//                return result;
//            }
//        }
        newBanner = mallBannerService.list(location, 1);
        result.setNewBanner(newBanner);
        return result;
    }

    @Override
    public NoticeHomeListVO noticeHomePageIOS(String appStoreVersion) {
        NoticeHomeListVO result = new NoticeHomeListVO();
        Integer isIosShow = this.getIsShowIOS(appStoreVersion);
        List<NoticeInfoVO> noticeInfos = Lists.newArrayList();
        if (isIosShow.intValue() == 1){
            NoticeInfoVO noticeInfoVO = new NoticeInfoVO();
            noticeInfoVO.setId(0L);
            noticeInfoVO.setCreateTime(DateTime.now());
            noticeInfoVO.setNoticeTitle("玛特元宇宙即将上线,敬请期待");
            noticeInfoVO.setNoticeContent("");
            noticeInfoVO.setNoticeType(1);
            noticeInfos.add(noticeInfoVO);
        }
        else {
            //公告
            noticeInfos =noticeInfoDao.getNoticeInfos();
        }
        result.setNoticeInfos(noticeInfos);
        return result;
    }

    @Override
    public DigitalCollectionHomeListVO digitalCollectionHomePageIOS(String appStoreVersion) {
        DigitalCollectionHomeListVO result = new DigitalCollectionHomeListVO();
        Integer isIosShow = this.getIsShowIOS(appStoreVersion);
        //首页推荐商品
        List<HomePageDigitalCollectionProductVO> top10DigitalCollection= digitalCollectionProductInfoService.getTop10DigitalCollection(isIosShow);
        for (HomePageDigitalCollectionProductVO digitalCollectionProductVO : top10DigitalCollection) {

            if(digitalCollectionProductVO.getTypeClassify() == ClassifyType.COLLECTION){
                DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductVO.getRelationId());
                digitalCollectionProductVO.setProductCount(digitalCollection.getSumCount());
            }else{
                ProductStock productStock = productStockDao.getProductStock(digitalCollectionProductVO.getProductId(),digitalCollectionProductVO.getRelationId(),null);
                digitalCollectionProductVO.setProductCount(productStock.getSumCount());
            }

            List timeList=new ArrayList();

            //region 首页申购
            if(digitalCollectionProductVO.getCommodityType() == CommodityType.SUBSCRIBE_COMMODITY) {
                DigitalCollectionProductInfoSubscribe digitalCollectionProductInfoSubscribe = digitalCollectionProductInfoSubscribeDao.getByProductInfoSubscribe(digitalCollectionProductVO.getProductId());
                if(Objects.nonNull(digitalCollectionProductInfoSubscribe)) {
                    timeList.add(DateUtil.format(digitalCollectionProductInfoSubscribe.getSubscribeStartTime(), "MM-dd HH:mm")+" 开始申购");
                    timeList.add(DateUtil.format(digitalCollectionProductInfoSubscribe.getConsistenceStartTime(), "MM-dd HH:mm")+" 开始抽签");
                    timeList.add(DateUtil.format(digitalCollectionProductInfoSubscribe.getConsistenceEndTime(), "MM-dd HH:mm")+" 公布结果");
                    if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())){
                        Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                        digitalCollectionProductVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                        String format = DateUtil.format(date, "MM-dd HH:mm");
                        timeList.add(format+" 开始购买");
                    }
                }
            }else if (digitalCollectionProductVO.getCommodityType() == CommodityType.AUCTION_PRODUCT) {
                DigitalCollectionProductInfoAuction digitalCollectionProductInfoAuction = digitalCollectionProductInfoAuctionDao.getByProductId(digitalCollectionProductVO.getProductId());
                if(Objects.nonNull(digitalCollectionProductInfoAuction)) {
                    Date date = new Date();
                    if (date.before(digitalCollectionProductInfoAuction.getAuctionStartTime())) {
                        timeList.add(DateUtil.format(digitalCollectionProductInfoAuction.getAuctionStartTime(), "MM-dd HH:mm")+" 开始拍卖");
                    }else if (date.after(digitalCollectionProductInfoAuction.getAuctionStartTime()) && date.before(digitalCollectionProductInfoAuction.getAuctionEndTime())) {
                        timeList.add("进行中");
                    }else {
                        timeList.add("已结束");
                    }
                }
            } else if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 发售");
            }
            
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getPriorityPurchaseBeginTime())
                && Objects.nonNull(digitalCollectionProductVO.getIsPriorityPurchase())
                && digitalCollectionProductVO.getIsPriorityPurchase()){
                Date date = DateUtil.parse(digitalCollectionProductVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setPriorityPurchaseBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先购");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getRushBuyBeginTime())
                && Objects.nonNull(digitalCollectionProductVO.getIsRushBuy())
                && digitalCollectionProductVO.getIsRushBuy()){
                Date date = DateUtil.parse(digitalCollectionProductVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setRushBuyBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先抢");
            }
            digitalCollectionProductVO.setList(timeList);
        }
        result.setTop10DigitalCollection(top10DigitalCollection);
        return result;
    }

    @Override
    public HomePageVO homePage(String appStoreVersion) {
        HomePageVO homePageVO=new HomePageVO();
//        UserInfo user = UserPool.getUser();
//        String token = redisUtil.get(RedisKeyEnum.USER_INFO_RELATION.getKey() + user.getId()).toString();
//        List<AdvertisementInfoVO> list = new ArrayList<>();
        //滚动广告图
//        List<AdvertisementInfo> advertisementInfos= advertisementInfoDao.getAdvertisementInfos();
//        for (AdvertisementInfo advertisementInfo : advertisementInfos) {
//            list.add(BeanUtil.toBean(advertisementInfo, AdvertisementInfoVO.class));
//        }

        List<MallBannerVO> mallBannerList = mallBannerService.list(1L, 1);
        Boolean isSm=false;
        int isIosShow = 0;
        UserInfo user = UserPool.getUser();
        if (ObjectUtil.isNotEmpty(user)){
            isSm=user.getIsSm();
            if(user.getUserTel().equals("13849904109") ){
                isIosShow =1;
            }
        }

        if(StrUtil.isNotBlank(appStoreVersion)){
            AppVersion ios = appVersionService.findOneByType("ios");
            if(ios.getAppStoreVersion().equals(appStoreVersion)){
                isIosShow =1;
            }
        }

        //公告
        List<NoticeInfoVO> noticeInfos =noticeInfoDao.getNoticeInfos();
        //盲盒系列
        List<ALLCatenaVO> catenaList=Lists.newArrayList();
//        List<ALLCatenaVO> catenaList= digitalCollectionProductInfoService.findBlindBoxCatena(isIosShow);
//        for (ALLCatenaVO allCatenaVO : catenaList) {
//            ProductStock productStock = productStockDao.getProductStock(allCatenaVO.getBlindBoxId(),allCatenaVO.getRelationId(),null);
//            allCatenaVO.setProductCount(productStock.getSumCount());
//            List timeList=new ArrayList();
//            if (ObjectUtil.isNotEmpty(allCatenaVO.getBeginTime())){
//                Date date = DateUtil.parse(allCatenaVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
//                allCatenaVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
//                String format = DateUtil.format(date, "MM-dd HH:mm");
//                timeList.add(format+" 发售");
//            }
//            if (ObjectUtil.isNotEmpty(allCatenaVO.getPriorityPurchaseBeginTime())){
//                Date date = DateUtil.parse(allCatenaVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
//                allCatenaVO.setPriorityPurchaseBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
//                String format = DateUtil.format(date, "MM-dd HH:mm");
//                timeList.add(format+" 优先购");
//            }
//            if (ObjectUtil.isNotEmpty(allCatenaVO.getRushBuyBeginTime())){
//                Date date = DateUtil.parse(allCatenaVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
//                allCatenaVO.setRushBuyBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
//                String format = DateUtil.format(date, "MM-dd HH:mm");
//                timeList.add(format+" 优先抢");
//            }
//            allCatenaVO.setList(timeList);
//        }
        //首页推荐商品
        List<HomePageDigitalCollectionProductVO> top10DigitalCollection= digitalCollectionProductInfoService.getTop10DigitalCollection(isIosShow);
        for (HomePageDigitalCollectionProductVO digitalCollectionProductVO : top10DigitalCollection) {

            if(digitalCollectionProductVO.getTypeClassify() == ClassifyType.COLLECTION){
                DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductVO.getRelationId());
                digitalCollectionProductVO.setProductCount(digitalCollection.getSumCount());
            }else{
                ProductStock productStock = productStockDao.getProductStock(digitalCollectionProductVO.getProductId(),digitalCollectionProductVO.getRelationId(),null);
                digitalCollectionProductVO.setProductCount(productStock.getSumCount());
            }

            List timeList=new ArrayList();
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 发售");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getPriorityPurchaseBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setPriorityPurchaseBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先购");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getRushBuyBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setRushBuyBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先抢");
            }
            digitalCollectionProductVO.setList(timeList);
        }
//        homePageVO.setAdvertisementInfos(list);
        //todo 2023年5月17日16:39:31 修改 隔几个版本后上面的代码要删除掉.
        homePageVO.setNewBanner(mallBannerList);
        homePageVO.setNoticeInfos(noticeInfos);
        homePageVO.setCatenaList(catenaList);
        homePageVO.setTop10DigitalCollection(top10DigitalCollection);


        homePageVO.setIsSm(isSm);

        UserHomeVO userHome = userInfoService.getUserHome();
        homePageVO.setUserHomeVO(userHome);
        return homePageVO;
    }

    @Override
    public HomeUserPageVO homeUserPage() {
        HomeUserPageVO result=new HomeUserPageVO();
        Boolean isSm=false;
        UserInfo user = UserPool.getUser();
        if (ObjectUtil.isNotEmpty(user)){
            isSm=user.getIsSm();
        }
        result.setIsSm(isSm);
        UserHomeVO userHome = userInfoService.getUserHome();
        result.setUserHomeVO(userHome);
        return result;
    }
    @Override
    public BannerHomeListVO bannerHomePage(Long location) {
        BannerHomeListVO result=new BannerHomeListVO();
        List<MallBannerVO> newBanner  = mallBannerService.list(location, 1);
//        Object object = redisUtil.get(RedisKeyEnum.HOME_BANNER.getKey());
//        if (ObjectUtil.isNotEmpty(object)){
//            List mallBanners = (List) JSONObject.parseArray(object.toString());
//            for (Object banner : mallBanners) {
//                MallBannerVO mallBannerVO = JSONObject.parseObject(banner.toString(), MallBannerVO.class);
//                newBanner.add(mallBannerVO);
//            }
//            result.setNewBanner(newBanner);
//            return result;
//        }
//        else {

//            String data = JSONObject.toJSON(newBanner).toString();
//            redisUtil.set(RedisKeyEnum.HOME_BANNER.getKey(), data, 30);
            result.setNewBanner(newBanner);
            return result;
//        }
    }

    @Override
    public NoticeHomeListVO noticeHomePage() {
        NoticeHomeListVO result=new NoticeHomeListVO();
        List<NoticeInfoVO> noticeInfos =noticeInfoDao.getNoticeInfos();
        result.setNoticeInfos(noticeInfos);
        return result;
    }

    @Override
    public DigitalCollectionHomeListVO digitalCollectionHomePage(String appStoreVersion) {
        DigitalCollectionHomeListVO result=new DigitalCollectionHomeListVO();
        int isIosShow = 0;
        UserInfo user = UserPool.getUser();
        if (ObjectUtil.isNotEmpty(user)){
            if(user.getUserTel().equals("13849904109") ){
                isIosShow =1;
            }
        }
        if(StrUtil.isNotBlank(appStoreVersion)){
            AppVersion ios = appVersionService.findOneByType("ios");
            if(ios.getAppStoreVersion().equals(appStoreVersion)){
                isIosShow =1;
            }
        }
        //首页推荐商品
        List<HomePageDigitalCollectionProductVO> top10DigitalCollection= digitalCollectionProductInfoService.getTop10DigitalCollection(isIosShow);
        for (HomePageDigitalCollectionProductVO digitalCollectionProductVO : top10DigitalCollection) {

            if(digitalCollectionProductVO.getTypeClassify() == ClassifyType.COLLECTION){
                DigitalCollection digitalCollection = digitalCollectionDao.getById(digitalCollectionProductVO.getRelationId());
                digitalCollectionProductVO.setProductCount(digitalCollection.getSumCount());
            }else{
                ProductStock productStock = productStockDao.getProductStock(digitalCollectionProductVO.getProductId(),digitalCollectionProductVO.getRelationId(),null);
                digitalCollectionProductVO.setProductCount(productStock.getSumCount());
            }

            List timeList=new ArrayList();

            //region 首页申购
            if(digitalCollectionProductVO.getCommodityType() == CommodityType.SUBSCRIBE_COMMODITY) {
                DigitalCollectionProductInfoSubscribe digitalCollectionProductInfoSubscribe = digitalCollectionProductInfoSubscribeDao.getByProductInfoSubscribe(digitalCollectionProductVO.getProductId());
                if(Objects.nonNull(digitalCollectionProductInfoSubscribe)) {
                    timeList.add(DateUtil.format(digitalCollectionProductInfoSubscribe.getSubscribeStartTime(), "MM-dd HH:mm")+" 开始申购");
                    timeList.add(DateUtil.format(digitalCollectionProductInfoSubscribe.getConsistenceStartTime(), "MM-dd HH:mm")+" 开始抽签");
                    timeList.add(DateUtil.format(digitalCollectionProductInfoSubscribe.getConsistenceEndTime(), "MM-dd HH:mm")+" 公布结果");
                    if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())){
                        Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                        digitalCollectionProductVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                        String format = DateUtil.format(date, "MM-dd HH:mm");
                        timeList.add(format+" 开始购买");
                    }
                }
            }else if (digitalCollectionProductVO.getCommodityType() == CommodityType.AUCTION_PRODUCT) {
                DigitalCollectionProductInfoAuction digitalCollectionProductInfoAuction = digitalCollectionProductInfoAuctionDao.getByProductId(digitalCollectionProductVO.getProductId());
                if(Objects.nonNull(digitalCollectionProductInfoAuction)) {
                    Date date = new Date();
                    if (date.before(digitalCollectionProductInfoAuction.getAuctionStartTime())) {
                        timeList.add(DateUtil.format(digitalCollectionProductInfoAuction.getAuctionStartTime(), "MM-dd HH:mm")+" 开始拍卖");
                    }else if (date.after(digitalCollectionProductInfoAuction.getAuctionStartTime()) && date.before(digitalCollectionProductInfoAuction.getAuctionEndTime())) {
                        timeList.add("进行中");
                    }else {
                        timeList.add("已结束");
                    }
                }
            } else if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getBeginTime())){
                Date date = DateUtil.parse(digitalCollectionProductVO.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 发售");
            }
            //endregion
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getPriorityPurchaseBeginTime())
                && Objects.nonNull(digitalCollectionProductVO.getIsPriorityPurchase())
                && digitalCollectionProductVO.getIsPriorityPurchase()){
                Date date = DateUtil.parse(digitalCollectionProductVO.getPriorityPurchaseBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setPriorityPurchaseBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先购");
            }
            if (ObjectUtil.isNotEmpty(digitalCollectionProductVO.getRushBuyBeginTime())
                && Objects.nonNull(digitalCollectionProductVO.getIsRushBuy())
                && digitalCollectionProductVO.getIsRushBuy()){
                Date date = DateUtil.parse(digitalCollectionProductVO.getRushBuyBeginTime(), "yyyy-MM-dd HH:mm:ss");
                digitalCollectionProductVO.setRushBuyBeginTime(DateUtil.format(date,"MM-dd HH:mm"));
                String format = DateUtil.format(date, "MM-dd HH:mm");
                timeList.add(format+" 优先抢");
            }
            digitalCollectionProductVO.setList(timeList);
        }
        result.setTop10DigitalCollection(top10DigitalCollection);
        return result;
    }

    //持仓排行
    @Override
    public HashMap findPositionRanking() {
        UserInfo user = UserPool.getUser();
        Object object = redisUtil.get(RedisKeyEnum.POSITION_RANKING.getKey());
        List<HashMap> list = new ArrayList<>();
        if (ObjectUtil.isEmpty(object)){
            List<HashMap> positionRanking = userInfoService.findPositionRanking();
            String positionRankingString = JSONObject.toJSON(positionRanking).toString();
            list=positionRanking;
          /*  HashMap hashMap= positionRanking.get(positionRanking.size() - 1);
            String positionNum = hashMap.get("positionNum").toString();*/
            redisUtil.set(RedisKeyEnum.POSITION_RANKING.getKey(),positionRankingString,5);
           // redisUtil.set(RedisKeyEnum.POSITION_RANKING_MIN_NUM.getKey(),positionNum,24*60);
        }else {
          list=(List) JSONObject.parseArray(object.toString());
        }
        for (Map map : list) {
            BigDecimal inviteNum = new BigDecimal(map.get("rankNo").toString());
            String s = inviteNum.stripTrailingZeros().toPlainString();
            map.put("rankNo",s);
        }
        HashMap userMap = userInfoService.findPositionRankingById(user.getId());
        userMap.put("list",list);
        String rankNo = userMap.get("rankNo").toString();
        if(ObjectUtil.isNotEmpty(rankNo)&&rankNo.contains(".")){
            BigDecimal inviteNum = new BigDecimal(userMap.get("rankNo").toString());
            String s = inviteNum.stripTrailingZeros().toPlainString();
            userMap.put("rankNo",s);
        }
        if (Integer.parseInt(userMap.get("positionNum").toString())<=0){
            userMap.put("rankNo","--");
        }
        return userMap;
    }
    @Override
    public void updatePositionRanking(Long userId) {
        UserInfo userInfo = userInfoDao.getById(userId);
        Object object = redisUtil.get(RedisKeyEnum.POSITION_RANKING_MIN_NUM.getKey());
        int i=0;
        if (ObjectUtil.isNotEmpty(object)){
            i=Integer.parseInt(object.toString());
        }
        //redis中key过期或者用户的持仓数量大于排行榜中最后一名的持仓数量则更新持仓排行榜
        if (ObjectUtil.isEmpty(object)||userInfo.getPositionNum()>i){
            List<HashMap> positionRanking = userInfoService.findPositionRanking();
            String positionRankingString = JSONObject.toJSON(positionRanking).toString();
            HashMap hashMap= positionRanking.get(positionRanking.size() - 1);
            String positionNum = hashMap.get("positionNum").toString();
            redisUtil.set(RedisKeyEnum.POSITION_RANKING.getKey(),positionRankingString,24*60);
            redisUtil.set(RedisKeyEnum.POSITION_RANKING_MIN_NUM.getKey(),positionNum,24*60);
        }
    }

    @Override
    public HashMap findInviteRanking() {
        UserInfo user = UserPool.getUser();
        Object object = redisUtil.get(RedisKeyEnum.INVITATION_RANKING.getKey());
        List<HashMap> list = new ArrayList<>();
        if (ObjectUtil.isEmpty(object)){
            List<HashMap> inviteRanking = userInfoService.findInviteRanking();
            String pinviteRankingString = JSONObject.toJSON(inviteRanking).toString();
            list=inviteRanking;
           /* HashMap hashMap= inviteRanking.get(inviteRanking.size() - 1);
            String inviteNum = hashMap.get("inviteNum").toString();*/
            redisUtil.set(RedisKeyEnum.INVITATION_RANKING.getKey(),pinviteRankingString,5);
            //redisUtil.set(RedisKeyEnum.INVITATION_RANKING_MIN_NUM.getKey(),inviteNum,24*60);
        }else {
            list=(List) JSONObject.parseArray(object.toString());
        }
        for (Map map : list) {
            BigDecimal inviteNum = new BigDecimal(map.get("rankNo").toString());
            String s = inviteNum.stripTrailingZeros().toPlainString();
            map.put("rankNo",s);
        }
        HashMap userMap = userInfoService.findInviteRankingById(user.getId());
        userMap.put("list",list);
        String rankNo = userMap.get("rankNo").toString();
        if(ObjectUtil.isNotEmpty(rankNo)&&rankNo.contains(".")){
            BigDecimal inviteNum = new BigDecimal(userMap.get("rankNo").toString());
            String s = inviteNum.stripTrailingZeros().toPlainString();
            userMap.put("rankNo",s);
        }
        if (Integer.parseInt(userMap.get("inviteNum").toString())<=0){
            userMap.put("rankNo","--");
        }
        return userMap;
    }

    @Override
    public void updateInviteRanking(Long userId) {
        UserInfo userInfo = userInfoDao.getById(userId);
        String s = (String) redisUtil.get(RedisKeyEnum.INVITATION_RANKING_MIN_NUM.getKey());
        int i=0;
        if (ObjectUtil.isNotEmpty(s)){
            i=Integer.parseInt(s);
        }
        //redis中key过期或者用户的有效邀请人数大于排行榜中最后一名的有效邀请人数则更新排行榜
        if (ObjectUtil.isEmpty(s)||userInfo.getValidInviteNum()>i){
            List<HashMap> inviteRanking = userInfoService.findInviteRanking();
            String pinviteRankingString = JSONObject.toJSON(inviteRanking).toString();
            HashMap hashMap= inviteRanking.get(inviteRanking.size() - 1);
            String inviteNum = hashMap.get("inviteNum").toString();
            redisUtil.set(RedisKeyEnum.INVITATION_RANKING.getKey(),pinviteRankingString,20*60);
            redisUtil.set(RedisKeyEnum.INVITATION_RANKING_MIN_NUM.getKey(),inviteNum,24*60);
        }
    }

    @Override
    public List<HashMap> findAllBlindBox() {
        UserInfo user = UserPool.getUser();
        int isIosShow = 0;
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getUserTel().equals("13849904109") ) {
                isIosShow = 1;
            }
        }
        return digitalCollectionProductInfoService.findAllBlindBox(isIosShow);
    }

    public Integer getIsShowIOS(String appStoreVersion){
        int isIosShow = 0;
        UserInfo user = UserPool.getUser();
        if (ObjectUtil.isNotEmpty(user) && user.getUserTel().equals("13849904109")){
            isIosShow =1;
        }
        if(StrUtil.isNotBlank(appStoreVersion)){
            AppVersion ios = appVersionService.findOneByType("ios");
            if(ios.getAppStoreVersion().equals(appStoreVersion)){
                isIosShow =1;
            }
        }
        return isIosShow;
    }



}
